/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.xtd.view.employment;

import com.xtd.facade.employment.DepartmentPostFacade;
import com.xtd.model.employment.DepartmentPost;
import com.xtd.view.AbstractManager;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.faces.model.SelectItem;
import javax.faces.validator.ValidatorException;

/**
 *
 * @author Lu Yuanbin
 */
@ManagedBean
@SessionScoped
public class DepartmentManager extends AbstractManager {

    @EJB
    DepartmentPostFacade departmentPostFacade;
    private List<DepartmentPost> departmentPosts;
    private List<SelectItem> departmentItems;
    private String department;
    private DepartmentPost selectedDepartmentPost;

    /** Creates a new instance of DepartmentManager */
    public DepartmentManager() {
    }

    /**
     * @return the departmentPosts
     */
    public List<DepartmentPost> getDepartmentPosts() {

        return departmentPosts;
    }

    /**
     * @param departmentPosts the departmentPosts to set
     */
    public void setDepartmentPosts(List<DepartmentPost> departmentPosts) {
        this.departmentPosts = departmentPosts;
    }

    /**
     * @return the departmentItems
     */
    public List<SelectItem> getDepartmentItems() {
        if (departmentItems == null) {

            initDepartmentItems();
        }
        return departmentItems;
    }

    /**
     * @param departmentItems the departmentItems to set
     */
    public void setDepartmentItems(List<SelectItem> departmentItems) {
        this.departmentItems = departmentItems;
    }

    /**
     * @return the department
     */
    public String getDepartment() {
        return department;
    }

    /**
     * @param department the department to set
     */
    public void setDepartment(String department) {
        this.department = department;
    }

    private void initDepartmentItems() {
        departmentItems = new ArrayList();
        List<String> departments = departmentPostFacade.findAllDepartments();
        for (String de : departments) {
            departmentItems.add(new SelectItem(de, de));
        }
    }

    public void searchByDepartment() {
        this.departmentPosts.clear();
        this.departmentPosts.addAll(this.departmentPostFacade.findByDepartment(department));
    }

    public void createDepartment(ActionEvent actionEvent) {
        selectedDepartmentPost = new DepartmentPost();
    }

    public void createPost(ActionEvent actionEvent) {

        if (department == null || department.isEmpty()) {
            addCallbackParam("success", false);
            addMessage("添加失败", "请选择部门后再添加职位", FacesMessage.SEVERITY_WARN);
            return;
        }
        selectedDepartmentPost = new DepartmentPost();
        selectedDepartmentPost.setName(department);
        addCallbackParam("success", true);
    }

    public void save(ActionEvent actionEvent) {
        if (departmentPostFacade.hasDuplicateDepartmentPost(selectedDepartmentPost, selectedDepartmentPost.isNew())) {
            addCallbackParam("success", false);
            addMessage("保存失败", "不可重复添加部门职位", FacesMessage.SEVERITY_WARN);
            return;
        }
        boolean isNew = selectedDepartmentPost.isNew();
        departmentPostFacade.update(selectedDepartmentPost);

        if (isNew && !selectedDepartmentPost.getName().equals(department)) {
            initDepartmentItems();
            department = selectedDepartmentPost.getName();
            this.departmentPosts.clear();
            departmentPosts.add(selectedDepartmentPost);
        }
        addMessage("保存成功", null, FacesMessage.SEVERITY_WARN);
        addCallbackParam("success", true);

    }

    /**
     * @return the selectedDepartmentPost
     */
    public DepartmentPost getSelectedDepartmentPost() {
        if (departmentPosts == null) {
            departmentPosts = new ArrayList();
        }
        return selectedDepartmentPost;
    }

    /**
     * @param selectedDepartmentPost the selectedDepartmentPost to set
     */
    public void setSelectedDepartmentPost(DepartmentPost selectedDepartmentPost) {

        this.selectedDepartmentPost = selectedDepartmentPost;
    }
}
